<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Validate;
use think\model\concern\SoftDelete;
use app\common\model\User as UserModel;

use think\facade\Log;

class DormUserRel extends Common
{
    //时间自动存储
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';
    //软删除位
    protected $deleteTime = 'isdel';

    protected $rule = [
        'dormId'   => 'require',
        'mobile'   => 'require',
    ];
    protected $msg = [
        'dormId'   => '请选择宿舍楼',
        'mobile'   => '请选择管理员',
    ];

    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->alias('rel')
                ->field($tableWhere['field']) 
                ->join('zl_dorm drom', 'drom.id = rel.dormId')
                ->join('zl_user user', 'user.mobile = rel.mobile')
                ->where($tableWhere['where'])
                ->order($tableWhere['order'])
                ->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    /**
     *  添加品牌
     * User:tony
     * @param $data
     * @return array
     */
    public function addData($post)
    {
        $data['dormId'] = $post['dormId'];
        $data['mobile'] = $post['mgrmobile'];
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data'=> []
        ];
        $validate = new Validate($this->rule,$this->msg);
        if (!$validate->check($data)) {
            $result['status'] = false;
            $result['msg'] = $validate->getError();
        } else {
            // 判断是否一个人管理多个宿舍
            $info = $this->field('dormName')
                ->alias('r')
                ->join('dorm d', 'r.dormId = d.id')
                ->where(['mobile'=>$data['mobile']])->find();
            
            if($info){
                $result['status'] = false;
                $result['msg'] = '该用户已是' . $info['dormName'] . '的管理员了！';   
            } else {
                $info['mobile'] = $data['mobile'];
                $info['dormId'] = $data['dormId']; 

                if (!$this->allowField(true)->save($info))
                {
                    $result['status'] = false;
                    $result['msg'] = '保存失败';
                }
            }
        }
        $result['token'] = \think\facade\Request::token('__ZlServer_Token__', 'sha1');
        return $result;
    }
 
    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        
        if(isset($post['dormId']) && $post['dormId'] != ""){
            $where[] = ['dormId', 'eq', $post['dormId']];
        }
        
        if(isset($post['dormName']) && $post['dormName'] != ""){
            $where[] = ['drom.dormName', 'like', '%' . $post['dormName'] . '%'];
        }
        
        if(isset($post['truename']) && $post['truename'] != ""){
            $where[] = ['user.truename', 'like', '%' . $post['truename'] . '%'];
        }
        
        if(isset($post['mobile']) && $post['mobile'] != ""){
            $where[] = ['rel.mobile', 'like', '%' . $post['mobile'] . '%'];
        }

        $result['where'] = $where;
        $result['field'] = "drom.dormName, rel.mobile, user.truename";
        $result['order'] = 'drom.sort, mobile';
        return $result;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }
}